package com.yunbao.core.respones;

import lombok.*;

import java.util.Objects;

/**
 * 统一API响应结果封装
 * 不要单独使用Result会编译出错,使用ResultBuilder方法调用 Result为返回
 *
 * @author 安能
 * @date 2019年6月24日10:56:38
 */
public final class Result<T> {

    /**
     * 状态码
     */
    private final   int code;

    /**
     * 响应消息
     */
    private final String message;

    /**
     * 响应数据
     */
    private final T data;

    private Result(final int code, final String message, final T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public int getCode() {
        return this.code;
    }

    public String getMessage() {
        return this.message;
    }

    public T getData() {
        return this.data;
    }
    static <T> Result.ResultBuilder<T> builder() {
        return new Result.ResultBuilder<>();
    }
    static class ResultBuilder<T> {
        private int code;
        private String message;
        private T data;

        ResultBuilder() {
        }

        Result.ResultBuilder<T> code(final int code) {
            this.code = code;
            return this;
        }

        Result.ResultBuilder<T> message(final String message) {
            this.message = message;
            return this;
        }

        Result.ResultBuilder<T> data(final T data) {
            this.data = data;
            return this;
        }

        Result<T> build() {
            return new Result<>(this.code, this.message, this.data);
        }

        public String toString() {
            return "Result.ResultBuilder(code=" + this.code + ", message=" + this.message + ", data=" + this.data + ")";
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Result<?> result = (Result<?>) o;
        return code == result.code &&
                Objects.equals(message, result.message) &&
                Objects.equals(data, result.data);
    }

    @Override
    public int hashCode() {
        return Objects.hash(code, message, data);
    }
}